package com.yiqi.lottery.feature.basketball.adapter;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.feature.basketball.view.BasketballBonusOptimizeActivity;
import com.yiqi.lottery.framework.DataServer;

import java.util.List;

public class BasketballBonusOptimizeAdapter extends BaseQuickAdapter<BasketballBonusOptimizeActivity.MatchCalculateInfo, BaseViewHolder> {

    private OnItemBeishuChangeListener mListener;

    public BasketballBonusOptimizeAdapter(OnItemBeishuChangeListener listener) {
        super(R.layout.item_youhua);
        mListener = listener;
    }

    @Override
    protected void convert(BaseViewHolder helper, final BasketballBonusOptimizeActivity.MatchCalculateInfo item) {
        String text = "";
        List<BallMatchBo.MatchInfo> matchInfos;
        int size = item.list.size();
        String[] numIndexs;
        for (int i = 0; i < size; ++i) {
            matchInfos = item.list.get(i);
            numIndexs = item.checkIndex.get(i).split("_");
            if (matchInfos.get(0).getPlay() == null) {
                text += matchInfos.get(0).getAn2() + "|" + numIndexs[2] + "|" + numIndexs[3] + "\n";
            } else {
                text += matchInfos.get(0).getPlay().getAn2() + "|" + numIndexs[2] + "|" + numIndexs[3] + "\n";
            }
        }
        if (size == 1) {
            text += "单关";
        } else {
            text += size + "串1";
        }
        helper.setText(R.id.tv_match_info, text)
                .setText(R.id.tv_prize, DataServer.getMoneyValue(item.money * item.currentBeishu) + "元")
                .setText(R.id.tv_beishu, String.valueOf(item.currentBeishu));
        View ivJia = helper.getView(R.id.btn_lottery_daletou_qishu_jia);
        ivJia.setTag(item);
        ivJia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BasketballBonusOptimizeActivity.MatchCalculateInfo item = (BasketballBonusOptimizeActivity.MatchCalculateInfo) view.getTag();
                item.currentBeishu += 1;
                TextView tv = helper.getView(R.id.tv_beishu);
                tv.setText(String.valueOf(item.currentBeishu));
                tv = helper.getView(R.id.tv_prize);
                tv.setText(DataServer.getMoneyValue(item.money * item.currentBeishu) + "元");
                if (mListener != null) {
                    mListener.onChange();
                }
            }
        });
        View ivJian = helper.getView(R.id.btn_lottery_daletou_qishu_jian);
        ivJian.setTag(item);
        ivJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BasketballBonusOptimizeActivity.MatchCalculateInfo item = (BasketballBonusOptimizeActivity.MatchCalculateInfo) view.getTag();
                if (item.currentBeishu == 0) {
                    return;
                }
                item.currentBeishu -= 1;
                TextView tv = helper.getView(R.id.tv_beishu);
                tv.setText(String.valueOf(item.currentBeishu));
                tv = helper.getView(R.id.tv_prize);
                tv.setText(DataServer.getMoneyValue(item.money * item.currentBeishu) + "元");
                if (mListener != null) {
                    mListener.onChange();
                }
            }
        });
        final EditText tv_beishu = helper.getView(R.id.tv_beishu);
        tv_beishu.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String text = editable.toString().trim();
                if (TextUtils.isEmpty(text) || text.startsWith("0")) {
                    item.tempBeishu = 1;
                } else {
                    item.tempBeishu = Integer.valueOf(text);
                }
//                if (mListener != null) {
//                    mListener.onChange();
//                }
            }
        });
    }

    public interface OnItemBeishuChangeListener {
        public void onChange();
    }
}
